$(document).ready(function() {
	var ChannelId=getItems("Channel") != null ? getItems("Channel") : "mychannel";
	var FabricFlag=getItems("networkName");
	var Token=getItems("token");

	if(loginToken){
		// console.log(ChannelId+"---"+FabricFlag);
		$(".Reminder").hide();
		if(getUrlParam('StartIndex') != null) {
			//查询块高度
			var oUrl = '/api/block/public/queryBlock';
			var queryBlockoData = {
				"RequestType": "QueryBlockByNumReq",
				"ChannelId": ChannelId,
				"FabricFlag":FabricFlag,
				"QueryBlockInfo":{
					"QueryType": 1,
					"StartIndex": parseInt(getUrlParam('StartIndex')),
					"Count": 1
				}
			};
			$(".cover").show();
			requestPost(oUrl, queryBlockoData, 0);
			$(".NoData").hide();
		} else if(getUrlParam('BlockHash') != null) {
			var oUrl = '/api/block/public/queryBlock';
			var queryBlockoData = {
				"RequestType": "QueryBlockByHashReq",
				"ChannelId": ChannelId,
				"FabricFlag":FabricFlag,
				"QueryBlockInfo":{
					"QueryType": 0,
					"BlockHash": getUrlParam('BlockHash'),
				}
			};
			$(".cover").show();
			requestPost(oUrl, queryBlockoData, 1);
			$(".NoData").hide();
		} else {
			// var oUrl = '/api/block/public/queryBlock';
			// var queryBlockoData = {
			// 	"RequestType": "QueryBlockByNumReq",
			// 	"ChannelId": ChannelId,
			// 	"FabricFlag":FabricFlag,
			// 	"QueryBlockInfo":{
			// 		"QueryType": 1,
			// 		"StartIndex": 1,
			// 		"Count":5
			// 	}
			// };
			// $(".cover").show();
			// requestPost(oUrl, queryBlockoData, 2);
		}
	}else{
		$(".Reminder").show();
		if(getUrlParam('StartIndex') != null) {
			//查询块高度
			var oUrl = '/api/block/cache/queryBlock';
			var queryBlockoData = {
				"RequestType": "QueryBlockByNumReq",
				"ChannelId": ChannelId,
				"FabricFlag":FabricFlag,
				"QueryBlockInfo":{
					"QueryType": 1,
					"StartIndex": parseInt(getUrlParam('StartIndex')),
					"Count": 1
				}
			};
			$(".cover").show();
			requestPost(oUrl, queryBlockoData, 0);
			$(".NoData").hide();
		} else if(getUrlParam('BlockHash') != null) {
			var oUrl = '/api/block/cache/queryBlock';
			var queryBlockoData = {
				"RequestType": "QueryBlockByHashReq",
				"ChannelId": ChannelId,
				"FabricFlag":FabricFlag,
				"QueryBlockInfo":{
					"QueryType": 0,
					"BlockHash": getUrlParam('BlockHash'),
				}
			};
			$(".cover").show();
			requestPost(oUrl, queryBlockoData, 1);
			$(".NoData").hide();
		} else {
			var oUrl = '/api/block/cache/queryBlock';
			var queryBlockoData = {
				"RequestType": "QueryBlockByNumReq",
				"ChannelId": ChannelId,
				"FabricFlag":FabricFlag,
				"QueryBlockInfo":{
					"QueryType": 1,
					"StartIndex":Number(1),
					"Count":5
				}
			};
			$(".cover").show();
			requestPost(oUrl, queryBlockoData, 2);
		}
	}

	
	function requestPost(url, Data, num) {
		var oData = Data;
		$.ajax({
			type: "POST",   
			contentType: "application/json",
			url: url,
			data: JSON.stringify(oData),
			dataType: "json",
			headers:{
				Authorization:Token,
			},
			success: function(data) {
				$(".cover").hide();
				switch(num) {
					case 0:
						if(data.code==200){
							if(data.status==40101){
								showHtml("登录已失效，请重新登录！");
							}else{
								var oData=JSON.parse(data.data);
								var str='';
								str+='<div class="blockListCt clear blockListCtActive"><div class="col-xs-2"><span>'+oData.BlockInfoList[0].BlockNum+'</span></div><div class="col-xs-8 text-center"><a>'+oData.BlockInfoList[0].CurrentBlockHash+'</a></div><div class="col-xs-2 text-right"><em>'+oData.BlockInfoList[0].TransactionList.length+'</em></div></div>';
								$(".blockList").append(str);
								$("#position").hide();
							}
							
						}else{
							showHtml("暂无数据")
						}
						break;
					case 1:
						if(data.code==200){
							if(data.status==40101){
								showHtml("登录已失效，请重新登录！");
							}else{
								var oData=JSON.parse(data.data);
								var str='';
								str+='<div class="blockListCt clear blockListCtActive"><div class="col-xs-2"><span>'+oData.BlockInfoList[0].BlockNum+'</span></div><div class="col-xs-8 text-center"><a>'+oData.BlockInfoList[0].CurrentBlockHash+'</a></div><div class="col-xs-2 text-right"><em>'+oData.BlockInfoList[0].TransactionList.length+'</em></div></div>';
								$(".blockList").append(str);
								$("#position").hide();
							}	
						}else{
							showHtml("暂无数据")
						}
						break;
					case 2:
						if(data.code==200){
							if(data.status==40101){
								showHtml("登录已失效，请重新登录！");
							}else{
								var oData=JSON.parse(data.data);
								console.log(oData)
								nodeStr(oData)
							}		
						}else{
							showHtml("暂无数据")
						}
						break;
					case 3:
						break;
					default:
						console.log('------');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				showHtml();
				$(".cover").hide();
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}
	
	function nodeStr(oData){
		var str='';
		for(var i=0;i<oData.BlockInfoList.length;i++){
			str+='<div class="blockListCt clear"><div class="col-xs-2"><span>'+oData.BlockInfoList[i].BlockNum+'</span></div><div class="col-xs-8 text-center"><a>'+oData.BlockInfoList[i].CurrentBlockHash+'</a></div><div class="col-xs-2 text-right"><em>'+oData.BlockInfoList[i].TransactionList.length+'</em></div></div>';
		}
		$(".blockList").append(str);
	}
	
	function showHtml(str){
		$(".containerBox").html('<div class="NoData">'+str+'</div>');
		$(".NoData").show();
		$(".NoData").css({
			marginTop:30
		})
	}
})